package com.imooc.momo_app;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private Button button, button1, button2;
    private int sum = 0;
    private CheckBox cb_game, cb_travel, cb_read;
    private Button btn_all, btn_notall, btn_getResult;
    private ButtonListener btnButtonListener;
    private List<String> lists;
    private TextView showResult;
    private RadioGroup radioGroup;
    private RadioButton rb_Male, rb_FeMale;
    private MyRadioButtonListener myRadioButtonListener;
    private ToggleButton toggleButton;
    private ImageView imageView;
    private CheckBoxListener checkBoxListener;
    private SeekBar seekBar;
    private ProgressBar progressBar, progressBar2;
    private Button btn_01;
    private Button btn_02;
    private RatingBar ratingBar;
    private TimePicker timePicker;
    private DatePicker datePicker;
    private TextView tv_content;
    private int Year, Month, Day, Hour, Minute;
    private Button startOther, layout_uidemo_button,asyncTaskButton;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*setContentView(R.layout.activity_main);
        textView = findViewById(R.id.helloworld);
        System.out.println("获取到的 TextView 为： " + textView.getText());
        textView.setText("为 TextView 重新设置值");
        button = findViewById(R.id.button);
        //为按钮添加监听器
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sum++;
                System.out.println("我被点击了 " + sum);
                textView.setText("我被点击了 " + sum);
            }
        });*/
        //按钮
        setContentView(R.layout.layout_demo);
        button1 = findViewById(R.id.btn01);
        button2 = findViewById(R.id.btn02);
        ButtonsListener buttonsListener = new ButtonsListener();
        button1.setOnClickListener(buttonsListener);
        button2.setOnClickListener(buttonsListener);
        //复选框
        initView();
        initDate();
        setListener();
        //单选按钮
        radioGroup = findViewById(R.id.rg_sex);
        rb_Male = findViewById(R.id.rb_Male);
        rb_FeMale = findViewById(R.id.rb_FeMale);
        myRadioButtonListener = new MyRadioButtonListener();
        radioGroup.setOnCheckedChangeListener(myRadioButtonListener);

        toggleButton = findViewById(R.id.tb);
        imageView = findViewById(R.id.imageView);
        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (toggleButton.isChecked()) {
                    Toast.makeText(MainActivity.this,
                            toggleButton.getText().toString(),
                            Toast.LENGTH_LONG).show();
                    imageView.setImageResource(R.drawable.happy);
                } else {
                    imageView.setImageResource(R.drawable.unhappy);
                }
            }
        });
        //seekBar
        seekBar = findViewById(R.id.seekbar);
        // 设置进度条最大值
        seekBar.setMax(100);
        // 设置进度条当前值
        seekBar.setProgress(30);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 将在结束拖拽进度条时被触发
                Log.i("progress", "结束 " + seekBar.getProgress() + "");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 将在开始拖拽进度条时被触发
                Log.i("progress", "开始 " + seekBar.getProgress() + "");
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                /*
                 * 将在进度发生变化时被触发 1、当前绑定的seekBar对象 2、当前进度数值 3、是否为用户手动触发
                 */
                Log.i("progress", seekBar.getProgress() + "");
            }
        });
        //progressBar
        initViewprogressBar();
        progressBar.setMax(100);
        progressBar.setProgress(30);
        progressBar.setSecondaryProgress(40);
        ButtonListenerprogressBar buttonListenerprogressBar = new ButtonListenerprogressBar();
        btn_01.setOnClickListener(buttonListenerprogressBar);
        btn_02.setOnClickListener(buttonListenerprogressBar);
        System.out.println("***判断当前长行进度条是否为不确定模式："
                + progressBar.isIndeterminate() + "***判断当前小型圆形进度条是否为不确定模式："
                + progressBar2.isIndeterminate());
        //ratingBar
        ratingBar = findViewById(R.id.ratingBar);
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating,
                                        boolean fromUser) {
                // 1、当前绑定的ratingBar对象
                // 2、当前rating评分的进度
                // 3、是否由用户触发
                System.out.println("当前ratingBar：评分 "
                        + rating
                        + " 是否来自用户： "
                        + fromUser
                        + " 每次评分的刻度 "
                        + ratingBar.getStepSize());
            }
        });
        //时间
        initViewTime();
        getCurrentTime();
        setPicker();
        //启动另外一个activity
        startOther = findViewById(R.id.button);
        startOther.setOnClickListener(new StartListener());
        layout_uidemo_button = findViewById(R.id.layout_uidemo_button);
        layout_uidemo_button.setOnClickListener(new StartListenerlayout_uidemo_button());
        asyncTaskButton = findViewById(R.id.asyncTaskButton);
        asyncTaskButton.setOnClickListener(new asyncTaskButton_button());
    }

    class asyncTaskButton_button implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // 当点击事件触发后执行，启动OtherActivity
            //创建一个Intent对象
            Intent intent = new Intent();
            //调用setClass方法指定启动某一个Activity
            intent.setClass(MainActivity.this, AsyncTaskActivity.class);
            //调用startActivity
            startActivity(intent);
        }

    }

    class StartListenerlayout_uidemo_button implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // 当点击事件触发后执行，启动OtherActivity
            //创建一个Intent对象
            Intent intent = new Intent();
            //调用setClass方法指定启动某一个Activity
            intent.setClass(MainActivity.this, UIDemoActivity.class);
            //调用startActivity
            startActivity(intent);
        }

    }

    class StartListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {

            // 当点击事件触发后执行，启动OtherActivity
            //创建一个Intent对象
            Intent intent = new Intent();
            //调用setClass方法指定启动某一个Activity
            intent.setClass(MainActivity.this, OtherActivity.class);
            //调用startActivity
            startActivity(intent);

        }

    }

    private void getCurrentTime() {
        // 获取系统当前时间
        Calendar calendar = Calendar.getInstance();
        Year = calendar.get(Calendar.YEAR);
        Month = calendar.get(Calendar.MONTH);
        Day = calendar.get(Calendar.DAY_OF_MONTH);
        Hour = calendar.get(Calendar.HOUR);
        Minute = calendar.get(Calendar.MINUTE);
    }

    private void initViewTime() {
        timePicker = (TimePicker) findViewById(R.id.timePicker);
        datePicker = (DatePicker) findViewById(R.id.datePicker);
        tv_content = (TextView) findViewById(R.id.tv_content);
    }

    @SuppressLint("SetTextI18n")
    private void setPicker() {
        // 设置时间选择器为24小时制
        timePicker.setIs24HourView(true);
        // 设置时间选择器的当前时间
        timePicker.setCurrentHour(Hour);
        timePicker.setCurrentMinute(Minute);
        //为时间选择器添加监听器
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                // 当时间选择器发生改变时，触发
                System.out.println("From TimePicker : " + hourOfDay + " :"
                        + minute);
            }
        });
        //为日期选择器添加监听器
        datePicker.init(Year, Month, Day, new DatePicker.OnDateChangedListener() {

            @Override
            public void onDateChanged(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                // 当日期选择器发生改变时，触发
                System.out.println("From DatePicker : " + year + "/"
                        + (monthOfYear + 1) + "/" + dayOfMonth);
                setTitle("From DatePicker : " + year + "/" + (monthOfYear + 1)
                        + "/" + dayOfMonth);
            }
        });
        //将当前日期+时间显示到指定的TextView中
        tv_content.setText("当前日期+时间：" + Year + "/" + (Month + 1) + "/" + Day + " "
                + Hour + " : " + Minute);
    }

    private void initViewprogressBar() {
        progressBar = findViewById(R.id.progressBar);
        progressBar2 = findViewById(R.id.progressBar_small);
        btn_01 = findViewById(R.id.btn_01);
        btn_02 = findViewById(R.id.btn_02);
    }

    class ButtonListenerprogressBar implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_01:
                    progressBar.incrementProgressBy(20);
                    break;
                case R.id.btn_02:
                    progressBar.incrementSecondaryProgressBy(40);
                    break;
            }
        }

    }

    private void setListener() {
        checkBoxListener = new CheckBoxListener();
        cb_game.setOnCheckedChangeListener(checkBoxListener);
        cb_travel.setOnCheckedChangeListener(checkBoxListener);
        cb_read.setOnCheckedChangeListener(checkBoxListener);
        btnButtonListener = new ButtonListener();
        btn_all.setOnClickListener(btnButtonListener);
        btn_notall.setOnClickListener(btnButtonListener);
        btn_getResult.setOnClickListener(btnButtonListener);

    }

    private void initView() {

        cb_game = findViewById(R.id.cb_game);
        cb_travel = findViewById(R.id.cb_travel);
        cb_read = findViewById(R.id.cb_read);
        btn_all = findViewById(R.id.btn_all);
        btn_notall = findViewById(R.id.btn_notall);
        btn_getResult = findViewById(R.id.btn_getResult);
        showResult = findViewById(R.id.btn_showResult);
    }


    class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // 选中状态改变时被触发
            switch (checkedId) {
                case R.id.rb_FeMale:
                    // 当用户选择女性时
                    Log.i("sex", "当前用户选择： " + R.id.rb_FeMale + rb_FeMale.getText().toString());
                    break;
                case R.id.rb_Male:
                    // 当用户选择男性时
                    Log.i("sex", "当前用户选择： " + R.id.rb_Male + rb_Male.getText().toString());
                    break;

            }

        }

    }

    class ButtonListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_all:
                    cb_game.setChecked(true);
                    cb_read.setChecked(true);
                    cb_travel.setChecked(true);
                    break;
                case R.id.btn_notall:
                    cb_game.setChecked(false);
                    cb_read.setChecked(false);
                    cb_travel.setChecked(false);
                    break;
                case R.id.btn_getResult:
                    if (cb_game.isChecked()) {
                        lists.add(cb_game.getText().toString());
                    }
                    if (cb_read.isChecked()) {
                        lists.add(cb_read.getText().toString());
                    }
                    if (cb_travel.isChecked()) {
                        lists.add(cb_travel.getText().toString());
                    }
                    showResult.setText(lists.toString());
                    lists.clear();
                    break;
            }

        }

    }

    class CheckBoxListener implements CompoundButton.OnCheckedChangeListener {
        @SuppressLint({"WrongConstant", "ShowToast"})
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // 当选中状态发生变化时触发
            CheckBox checkBox = (CheckBox) buttonView;
            switch (checkBox.getId()) {
                case R.id.cb_game:
                    if (isChecked) {
                        Toast.makeText(MainActivity.this, "少玩游戏多写代码！", 1000).show();
                        cb_game.setTextColor(Color.RED);
                    } else {
                        cb_game.setTextColor(Color.BLACK);
                    }
                    Toast.makeText(MainActivity.this, "电竞：" + isChecked, 1000)
                            .show();
                    break;
                case R.id.cb_travel:
                    Toast.makeText(MainActivity.this, "旅游：" + isChecked, 1000)
                            .show();
                    break;
                case R.id.cb_read:
                    Toast.makeText(MainActivity.this, "阅读：" + isChecked, 1000)
                            .show();
                    break;

            }
        }
    }

    class ButtonsListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn01:
                    Toast.makeText(MainActivity.this, "已点击第一个按钮",
                            Toast.LENGTH_LONG).show();
                    break;
                case R.id.btn02:
                    Toast.makeText(MainActivity.this, "已点击第二个按钮",
                            Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }

    private void initDate() {
        // 初始化数据
        lists = new ArrayList<String>();

    }
}
